if (typeof Object.assign != 'function') {
  Object.assign = function(target, varArgs) {
    'use strict';
    if (target == null) { // TypeError if undefined or null
      throw new TypeError('Cannot convert undefined or null to object');
    }

    var to = Object(target);

    for (var index = 1; index < arguments.length; index++) {
      var nextSource = arguments[index];

      if (nextSource != null) { // Skip over if undefined or null
        for (var nextKey in nextSource) {
          // Avoid bugs when hasOwnProperty is shadowed
          if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
            to[nextKey] = nextSource[nextKey];
          }
        }
      }
    }
    return to;
  };
}
var notieComponent = React.createClass({
	componentWillMount: function() {
        this.key = 0;
        this.state = {};
        this.wasMounted = false;
	},
    componentDidMount: function () {
        this.wasMounted = true;
    },
    getInitialState: function () {
        return {};
    },
    init: function (title, speaker, content, duration, link) {
        this.createNotie('', title, speaker, content, duration, link);
    },
    success: function (title, speaker, content, duration, link) {
        this.createNotie('success', title, speaker, content, duration, link);
    },
    error: function (title, speaker, content, duration, link) {
        this.createNotie('error', title, speaker, content, duration, link);
    },
    info: function (title, speaker, content, duration, link) {
        this.createNotie('info', title, speaker, content, duration, link);
    },
    /**
     * 创建通知弹框
     * @params
     * type: init, success, error, info
     * title: 提示标题
     * speaker: 提示人
     * content: 消息内容
     * duration: 显示时间(ms)
     * link: 消息跳转链接
     */
    createNotie: function (type, title, speaker, content, duration, link) {
        var key = this.key++;
        var _this = this;
        // var state = Object.assign(this.state, { [key]: { type, title, speaker, content, duration, link } });
        
        var list = [];
        for(var i = 0; i <= key; i++) {
            list.push({ type: type, title: title, speaker: speaker, content: content, duration: duration, link: link });
        }
        var obj = list.map(function(item, index) {
            return item;
        })
        var state = Object.assign(this.state, obj);

        this.setState(state, function() {
            return _this.closeNotie(duration, key)
        });
    },
    closeNotie: function (duration, key) {
        var _this = this;
        setTimeout(function() {
            _this.handleClose(key);
        }, duration);
    },
    handleClose: function (key) {
        if (!this.wasMounted) return;
        delete this.state[key];
        this.setState(this.state);
    },
    item: function (key) {
        var data = this.state[key];
        var _this = this;
        return (
            <div key={key} className={'notie-item bounceInUp ' + data.type}>
                <div className="notie-title">
                    <i className="icon icon-alert"></i>
                    {data.title}
                    <i className="icon icon-close" onClick={(function(){return _this.handleClose(key)})}></i>
                </div>
                <a href={data.link}>
                    <dl className="notie-content">
                        <dt>{data.speaker}：</dt>
                        <dd>{data.content}</dd>
                    </dl>
                </a>
            </div>
        )
    },
    render:function() {
        var keys = Object.keys(this.state);
        var _this = this;
        var el = keys.map(function(key) {
            return _this.item(key);
        })
        return <div className="notie-container">{el}</div>;
    }
});
module.exports = notieComponent;